<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="filters.name" placeholder="名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="filters.author" placeholder="作者" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="filters.tag" placeholder="标签" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <movie-level-select v-model="filters.movieLevelId" />
      <el-radio-group v-model="filters.fileDeleted" style="margin: 0 3px;">
        <el-radio-button :label="0" @click.native.prevent="fileDeletedFilter(0)">文件未删除</el-radio-button>
        <el-radio-button :label="1" @click.native.prevent="fileDeletedFilter(1)">文件已删除</el-radio-button>
      </el-radio-group>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      border
      fit
      :height="tableHeight"
      :data="list"
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
      @selection-change="selsChange"
    >
      <el-table-column type="index" width="60" />
      <el-table-column prop="name" label="名称" min-width="160" sortable />
      <el-table-column prop="author" label="作者" width="110" sortable  />
      <el-table-column prop="version" label="版本" width="100" sortable  />
      <el-table-column prop="tag" label="标签" min-width="160" :formatter="formatTag" />
      <el-table-column prop="publishTime" label="发布时间" width="160" sortable/>
      <el-table-column prop="playTimes" label="开本次数" width="110" sortable />
      <el-table-column prop="status" label="状态" width="70" :formatter="formatStatus" />
      <el-table-column prop="movieLevelName" label="类别" width="90" />
      <el-table-column prop="fileDeleted" label="文件删除状态" width="120" :formatter="formatDeleteStatus" />
      <el-table-column label="操作" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.fileDeleted===0" size="small" type="danger" @click="handleDeleteVersion(scope.index, scope.row)">删除剧本文件</el-button>
          <el-button v-if="scope.row.fileDeleted===1" size="small" type="danger" @click="handleDeleteInstance(scope.index, scope.row)">删除剧本记录</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filters.page" :limit.sync="filters.limit" @pagination="loadPageList" />
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'
import { movieVersions, deleteMovieFiles, deleteMovieInstance } from '@/api/movie'
import MovieLevelSelect from '@/views/components/movie-level-select'

export default {
  name: 'MovieList',
  components: { MovieLevelSelect, Pagination },
  directives: { waves },
  data() {
    return {
      tableHeight: 260,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      sels: [], // 列表选中列
      filters: {
        page: 1,
        limit: 20,
        name: '',
        author: null,
        tag: null,
        movieLevelId: null,
        fileDeleted: 0
      },
      orderBy: {
        orderColumn: '',
        order: ''
      }
    }
  },
  created() {
    this.tableHeight = window.innerHeight - 260
    this.loadPageList()
  },
  methods: {
    formatTag(row) {
      if (row.tag && Array.isArray(row.tag)) {
        return row.tag.join(' ')
      }
    },
    formatStatus(row) {
      const st = parseInt(row.status)
      if (st === 0) {
        return '已上线'
      } else if (st === 1) {
        return '已下线'
      }
      return ''
    },
    formatDeleteStatus(row) {
      if (row.fileDeleted) {
        return '已删除'
      }
      return '未删除'
    },
    loadPageList() {
      this.listLoading = true
      const param = { ...this.filters, ...this.orderBy }
      movieVersions(param).then(({ success, message, data }) => {
        this.listLoading = false
        if (success) {
          const { records, total } = data || {}
          this.list = records
          this.total = total
        } else {
          this.$message({
            message: message || '查询列表失败',
            type: 'error'
          })
        }
      }, () => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.filters.page = 1
      this.loadPageList()
    },
    fileDeletedFilter(fileDeleted) {
      if (fileDeleted === this.filters.fileDeleted) {
        this.filters.fileDeleted = null
      } else {
        this.filters.fileDeleted = fileDeleted
      }
      this.handleFilter()
    },
    sortChange({ prop, order }) {
      this.orderBy.orderColumn = prop
      this.orderBy.order = order
      this.handleFilter()
    },
    selsChange(sels) {
      this.sels = sels
    },
    // 删除剧本的某个版本
    handleDeleteVersion(index, row) {
      this.$confirm('确认删除该版本的剧本文件吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        deleteMovieFiles(row.movieInstanceId).then(({ success, message }) => {
          this.listLoading = false
          if (success) {
            this.$message({
              message: message || '删除成功',
              type: 'success'
            })
            this.loadPageList()
          } else {
            this.$message({
              message: message || '删除失败',
              type: 'error'
            })
          }
        }, (res) => {
          this.listLoading = false
        })
      })
    },
    handleDeleteInstance(index, row) {
      this.$confirm('确认删除该版本的剧本记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        deleteMovieInstance(row.movieInstanceId).then(({ success, message }) => {
          this.listLoading = false
          if (success) {
            this.$message({
              message: message || '删除成功',
              type: 'success'
            })
            this.loadPageList()
          } else {
            this.$message({
              message: message || '删除失败',
              type: 'error'
            })
          }
        }, (res) => {
          this.listLoading = false
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
